<template>
  <div class="family-accident">
    <div class="header">
      <h2>事故记录</h2>
      <p>查看我家老人的事故记录信息</p>
    </div>

    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>事故记录列表</span>
          <el-button type="primary" @click="loadAccidentRecords" :loading="loading">
            <el-icon><Refresh /></el-icon>
            刷新
          </el-button>
        </div>
      </template>

      <el-table :data="accidentRecords" style="width: 100%" v-loading="loading">
        <el-table-column prop="accidentDate" label="发生日期" width="120">
          <template #default="{ row }">
            {{ formatDate(row.accidentDate) }}
          </template>
        </el-table-column>
        <el-table-column prop="elderName" label="老人姓名" width="100"></el-table-column>
        <el-table-column prop="accidentType" label="事故类型" width="120">
          <template #default="{ row }">
            <el-tag :type="getAccidentTypeTag(row.accidentType)">
              {{ getAccidentTypeText(row.accidentType) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="severity" label="严重程度" width="100">
          <template #default="{ row }">
            <el-tag :type="getSeverityTag(row.severity)">
              {{ getSeverityText(row.severity) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="location" label="发生地点" width="120"></el-table-column>
        <el-table-column prop="description" label="事故描述" min-width="200" show-overflow-tooltip></el-table-column>
        <el-table-column prop="handlingMeasures" label="处理措施" min-width="200" show-overflow-tooltip></el-table-column>
        <el-table-column prop="caregiverName" label="处理护工" width="100"></el-table-column>
        <el-table-column label="操作" width="100" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" link @click="viewDetail(row)">
              查看详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 详情对话框 -->
    <el-dialog
      v-model="detailVisible"
      title="事故记录详情"
      width="700px"
    >
      <el-descriptions :column="2" border v-if="currentRecord">
        <el-descriptions-item label="老人姓名">{{ currentRecord.elderName }}</el-descriptions-item>
        <el-descriptions-item label="发生日期">{{ formatDate(currentRecord.accidentDate) }}</el-descriptions-item>
        <el-descriptions-item label="事故类型">
          <el-tag :type="getAccidentTypeTag(currentRecord.accidentType)">
            {{ getAccidentTypeText(currentRecord.accidentType) }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="严重程度">
          <el-tag :type="getSeverityTag(currentRecord.severity)">
            {{ getSeverityText(currentRecord.severity) }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="发生地点">{{ currentRecord.location }}</el-descriptions-item>
        <el-descriptions-item label="发生时间">{{ currentRecord.accidentTime || '未记录' }}</el-descriptions-item>
        <el-descriptions-item label="处理护工">{{ currentRecord.caregiverName }}</el-descriptions-item>
        <el-descriptions-item label="护工电话">{{ currentRecord.caregiverPhone }}</el-descriptions-item>
        <el-descriptions-item label="事故描述" :span="2">{{ currentRecord.description }}</el-descriptions-item>
        <el-descriptions-item label="处理措施" :span="2">{{ currentRecord.handlingMeasures }}</el-descriptions-item>
        <el-descriptions-item label="后续跟进" :span="2">{{ currentRecord.followUp || '无' }}</el-descriptions-item>
        <el-descriptions-item label="是否通知家属">
          <el-tag :type="currentRecord.notifiedFamily ? 'success' : 'warning'">
            {{ currentRecord.notifiedFamily ? '已通知' : '未通知' }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="通知时间">{{ currentRecord.notificationTime ? formatDateTime(currentRecord.notificationTime) : '未通知' }}</el-descriptions-item>
        <el-descriptions-item label="备注" :span="2">{{ currentRecord.notes || '无' }}</el-descriptions-item>
      </el-descriptions>
      <template #footer>
        <el-button @click="detailVisible = false">关闭</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { accidentApi } from '../../api/accident'

const loading = ref(false)
const accidentRecords = ref([])
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)
const detailVisible = ref(false)
const currentRecord = ref(null)

// 加载事故记录
const loadAccidentRecords = async () => {
  try {
    loading.value = true
    const response = await accidentApi.getFamilyAccidentRecords({
      page: currentPage.value,
      size: pageSize.value
    })
    if (response.code === 200) {
      accidentRecords.value = response.data.records || []
      total.value = response.data.total || 0
    } else {
      ElMessage.error(response.message || '加载失败')
    }
  } catch (error) {
    console.error('加载事故记录失败:', error)
    ElMessage.error('加载事故记录失败')
  } finally {
    loading.value = false
  }
}

// 查看详情
const viewDetail = (record) => {
  currentRecord.value = record
  detailVisible.value = true
}

// 格式化日期
const formatDate = (dateString) => {
  if (!dateString) return ''
  const date = new Date(dateString)
  return date.toLocaleDateString('zh-CN')
}

// 格式化日期时间
const formatDateTime = (dateString) => {
  if (!dateString) return ''
  const date = new Date(dateString)
  return date.toLocaleString('zh-CN')
}

// 获取事故类型标签类型
const getAccidentTypeTag = (type) => {
  const typeMap = {
    'FALL': 'danger',
    'ILLNESS': 'warning',
    'INJURY': 'warning',
    'OTHER': 'info'
  }
  return typeMap[type] || 'info'
}

// 获取事故类型文本
const getAccidentTypeText = (type) => {
  const typeMap = {
    'FALL': '跌倒',
    'ILLNESS': '突发疾病',
    'INJURY': '受伤',
    'OTHER': '其他'
  }
  return typeMap[type] || type
}

// 获取严重程度标签类型
const getSeverityTag = (severity) => {
  const severityMap = {
    'MINOR': 'info',
    'MODERATE': 'warning',
    'SEVERE': 'danger',
    'CRITICAL': 'danger'
  }
  return severityMap[severity] || 'info'
}

// 获取严重程度文本
const getSeverityText = (severity) => {
  const severityMap = {
    'MINOR': '轻微',
    'MODERATE': '中度',
    'SEVERE': '严重',
    'CRITICAL': '危急'
  }
  return severityMap[severity] || severity
}

// 分页处理
const handleSizeChange = (newSize) => {
  pageSize.value = newSize
  currentPage.value = 1
  loadAccidentRecords()
}

const handleCurrentChange = (newPage) => {
  currentPage.value = newPage
  loadAccidentRecords()
}

onMounted(() => {
  loadAccidentRecords()
})
</script>

<style scoped>
.family-accident {
  padding: 20px;
}

.header {
  margin-bottom: 20px;
}

.header h2 {
  margin: 0;
  color: #303133;
}

.header p {
  margin: 5px 0 0 0;
  color: #909399;
  font-size: 14px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>
